/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Data;
using System.IO;
using MyPhotoIndex.Interfaces;
using MyPhotoIndex.Utilities;
using MyPhotoIndex.Interfaces.Tagging;
using MyPhotoIndex.Plugins.DataProviders;
using System.Xml.Serialization;

namespace MyPhotoIndex.ImageBank
{
    [Serializable]
    public class ImageTag : IImageTag
    {
        List<IImageTag>     m_childTags;

        private bool        m_isRoot;
        private int         m_id;
        private string      m_name;
        private string      m_description;
        private DateTime    m_referenceDate = DateTime.Now;
        private String      m_path;
        [NonSerialized]
        private Image m_thumbnail;
        private IImageTag   m_parent;
        private int         m_offset;
        private bool        m_uniqueName = true;
        private string      m_keyword;
        private string      m_physicalFolder;

        public int Offset
        {
            get { return m_offset; }
            set { m_offset = value; }
        }

        [XmlIgnore]
        public IImageTag Parent
        {
            get { return m_parent; }
            set 
            { 
                m_parent = value;
                m_path = BuildPath();
            }
        }

        public ImageTag()
        {
            m_childTags = new List<IImageTag>();
        }

        public ImageTag(int id, string name, string description, Image thumbnail)
        {
            m_childTags = new List<IImageTag>();

            m_id = id;
            m_name = name.Substring(0, Math.Min(name.Length, 50));
            m_description = description;
            m_thumbnail = thumbnail;

            m_path = BuildPath();
        }

        public ImageTag(DataRow tagRow)
        {
            m_childTags = new List<IImageTag>();

            m_id = Convert.ToInt32(tagRow[(int)TagsDataSourceColumnId.Id]);
            if (tagRow.IsNull((int)TagsDataSourceColumnId.Name) == false)
            {
                m_name = Convert.ToString(tagRow[(int)TagsDataSourceColumnId.Name]);
            }

            if (tagRow.IsNull((int)TagsDataSourceColumnId.Description) == false)
            {
                m_description = Convert.ToString(tagRow[(int)TagsDataSourceColumnId.Description]);
            }

            m_isRoot = (Convert.ToInt32(tagRow[(int)TagsDataSourceColumnId.Parent]) <= 0) ? true : false;
            try
            {
                if (tagRow.IsNull((int)TagsDataSourceColumnId.Thambnail) == false)
                {
                    m_thumbnail = ThumbnailUtilities.ThumbnailFromByteArray((Byte[])tagRow[(int)TagsDataSourceColumnId.Thambnail]);
                }
            }
            catch (Exception ex)
            {
                string message = ex.Message;
            }

            if (tagRow.IsNull((int)TagsDataSourceColumnId.Keyword) == false)
            {
                m_keyword = Convert.ToString(tagRow[(int)TagsDataSourceColumnId.Keyword]);
            }

            if (tagRow.IsNull((int)TagsDataSourceColumnId.PhysicalFolder) == false)
            {
                m_physicalFolder = Convert.ToString(tagRow[(int)TagsDataSourceColumnId.PhysicalFolder]);
            }

            if (tagRow.IsNull((int)TagsDataSourceColumnId.Offset) == false)
            {
                m_offset = Convert.ToInt32 (tagRow[(int)TagsDataSourceColumnId.Offset]);
            }

            if (tagRow.IsNull((int)TagsDataSourceColumnId.ReferenceDate) == false)
            {
                m_referenceDate = Convert.ToDateTime(tagRow[(int)TagsDataSourceColumnId.ReferenceDate]);
            }

            m_path = BuildPath();
        }

        private String BuildPath()
        {
            return String.Concat(BuildPath(m_parent), (m_parent == null) ? String.Empty : "\\", Name);
        }

        private String BuildPath(IImageTag parent)
        {
            if (parent == null)
            {
                return String.Empty;
            }

            String path = parent.Name;
            if (parent.Parent != null)
            {
                path = String.Concat(BuildPath(parent.Parent), "\\", path);
            }

            return path;
        }

        [XmlIgnore]
        public bool IsRoot
        {
            get { return m_isRoot; }
            set { m_isRoot = value; }
        }

        public int Id
        {
            get { return m_id; }
            set { m_id = value; }
        }

        public string Name
        {
            get { return m_name; }
            set 
            {
                m_name =  value.Substring(0, Math.Min(value.Length, 50));
                m_path = BuildPath();
            }
        }

        public bool IsNameValid(string value)
        {
            return !string.IsNullOrEmpty(value);
        }

        public string Description
        {
            get { return m_description; }
            set { m_description = value; }
        }

        public DateTime ReferenceDate
        {
            get { return m_referenceDate; }
            set { m_referenceDate = value; }
        }

        [XmlIgnore]
        public Image Thumbnail
        {
            get { return m_thumbnail; }
            set { m_thumbnail = value; }
        }

        [XmlIgnore]
        public List<IImageTag> ChildTags
        {
            get { return m_childTags; }
        }

        public String Path 
        {
            get { return m_path; }
        }

        public override string ToString()
        {
            return this.Name;
        }

        [XmlIgnore]
        public bool UniqueName
        {
            get { return m_uniqueName; }
            set { m_uniqueName = value; }
        }

        public string Keyword
        {
            get { return m_keyword; }
            set { m_keyword = value; }
        }

        public string PhysicalFolder
        {
            get { return m_physicalFolder; }
            set { m_physicalFolder = value; }
        }
    }
}
